package org.platform.common.result;

import java.util.Date;
import java.util.UUID;

/**
 * <pre>
 * 功能：业务返回对象
 * </pre>
 * 
 * @author wei.gao
 * @param <T>
 */
public class ServiceResponse<T> {
	
	/**
	 * 请求编号
	 */
	private final String requestId = UUID.randomUUID().toString();
	
	public String getRequestId() {
		return requestId;
	}
	
	/**
	 * 请求开始时间
	 */
	private final long requestStartTime = new Date().getTime();
	
	public long getRequestStartTime() {
		return requestStartTime;
	}

	/**
	 * 请求结束时间
	 */
	protected long requestEndTime;
	
	public long getRequestEndTime() {
		return requestEndTime;
	}
	
	/**
	 * 相应代码
	 */
	protected String code = "10000";
	
	public String getCode() {
		return code;
	}
	
	/**
	 * 相应提示
	 */
	protected String message = "ok";
	
	public String getMessage() {
		return message;
	}
	
	/**
	 * 消息内容
	 */
	protected T data;
	
	public T getData() {
		return data;
	}
	
	/**
	 * 扩增参数
	 */
	protected Object params;
	
	public Object getParams() {
		return params;
	}

	public void setParams(Object params) {
		this.params = params;
	}

	/**
	 * <pre>
	 * 功能：接口调用成功，不需要返回对象
	 * </pre>
	 * @return
	 */
	public ServiceResponse<T> success() {
		this.code = "10000";
		this.message = "ok";
		this.requestEndTime = System.currentTimeMillis();
		this.data = null;
		return this;
	}
	
	/**
	 * <pre>
	 * 功能：接口调用成功，有返回对象
	 * </pre>
	 * @param object
	 * @return
	 */
	public ServiceResponse<T> success(T object) {
		this.code = "10000";
		this.message = "ok";
		this.requestEndTime = System.currentTimeMillis();
		this.data = object;
		return this;
	}
	
	/**
	 * <pre>
	 * 功能：接口调用失败，有错误码和描述，没有返回对象
	 * </pre>
	 * @param code
	 * @param message
	 * @return
	 */
	public ServiceResponse<T> failure(String code, String message) {
		this.code = code;
		this.message = message;
		this.requestEndTime = System.currentTimeMillis();
		this.data = null;
		return this;
	}
	
}